package com.pingtai.financial.service;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.pingtai.dao.AbstractManager;
import com.pingtai.financial.dao.FinancialDao;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;

public class FinancialService {

	private FinancialDao financialDao;
	private AbstractManager abstractManager;
	private WeiboIndentService weiboIndentService;
	private UserManagerService userManagerService;
	
	
	
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}



	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}



	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}



	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}



	public FinancialDao getFinancialDao() {
		return financialDao;
	}



	public void setFinancialDao(FinancialDao financialDao) {
		this.financialDao = financialDao;
	}



	public AbstractManager getAbstractManager() {
		return abstractManager;
	}



	public void setAbstractManager(AbstractManager abstractManager) {
		this.abstractManager = abstractManager;
	}



	public PaginationSupport getFinancialRecords(int pageName,User user){
		Session session = financialDao.getSession();
		Criteria criteria = session.createCriteria(UserPay.class);
		criteria.add(Restrictions.eq("childUser.userId", user.getUserId())).addOrder(
				Order.desc("id"));
		return abstractManager.findPageByCriteria(criteria, pageName);
	}
	
	public List getBaseIndentTypeFromUserPayByIndentId(Integer indentId){
		return financialDao.getBaseIndentTypeFromUserPayByIndentId(indentId);
	}
	
	public PaginationSupport getFinancialRecords3(int pageName,User user){
		Session session = financialDao.getSession();
		Criteria criteria = session.createCriteria(UserPay.class);
		criteria.add(Restrictions.eq("childUser.userId", user.getUserId()))
				.add(Restrictions.eq("type2", 1)).addOrder(
				Order.desc("id"));
		PaginationSupport ps =  abstractManager.findPageByCriteria(criteria, pageName);
		//获得消费总额
		List list = weiboIndentService.getUserConsumptionTotalMoney(user);
    	Float money = 0f;
    	if(list!=null&&list.size()>0){
    		for(int i=0;i<list.size();i++){
    			UserIndentMoneyTmp moneyTmp = (UserIndentMoneyTmp) list.get(i);
    			UserIndent indent = moneyTmp.getUserIndent();
    			List l = getBaseIndentTypeFromUserPayByIndentId(indent.getId());
    			if(l==null){
    				continue;
    			}else{
    				UserBaseIndentType base = (UserBaseIndentType) l.get(0);
    				Boolean flag = (Boolean) l.get(1);
    				
    				if(base.getStateType().equals("default")&&!indent.getState().equals(3)){
        				money += indent.getTotalPrice();
        			}else if(base.getStateType().equals("renzheng")&&flag){
        				money += moneyTmp.getMoney();
        			}
    			}
    			
    		}
    	}
    	ps.setTotalCosumption(money);
    	
    	//获得充值总额
    	List chongzhi = userManagerService.findUserChongzhiList(user);
    	Float chongzhiMoney = 0f;
    	if(chongzhi!=null&&chongzhi.size()>0){
    		for(int i=0;i<chongzhi.size();i++){
    			UserPay userPay = (UserPay) chongzhi.get(i);
    			chongzhiMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotleChongzhiMoney(chongzhiMoney);
    	//获得扣款总额
    	List koukuan = userManagerService.findUserKoukuanList(user);
    	Float koukuanMoney = 0f;
    	if(koukuan!=null&&koukuan.size()>0){
    		for(int i=0;i<koukuan.size();i++){
    			UserPay userPay = (UserPay) koukuan.get(i);
    			koukuanMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotleKoukuanMoney(koukuanMoney);
    	//获得返利总额
    	List fanli = userManagerService.findUserfanliList(user);
    	Float fanliMoney = 0f;
    	if(fanli!=null&&fanli.size()>0){
    		for(int i=0;i<fanli.size();i++){
    			UserPay userPay = (UserPay) fanli.get(i);
    			fanliMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotlefanliMoney(fanliMoney);
		return ps;
		
	}
	
	
	public PaginationSupport getFinancialRecords4(int pageName,User user){
		Session session = financialDao.getSession();
		Criteria criteria = session.createCriteria(UserPay.class);
		criteria.add(Restrictions.eq("childUser.userId", user.getUserId()))
				.add(Restrictions.eq("type", 0)).addOrder(
				Order.desc("id"));
		PaginationSupport ps =  abstractManager.findPageByCriteria(criteria, pageName);
		//获得消费总额
		List list = weiboIndentService.getUserConsumptionTotalMoney(user);
    	Float money = 0f;
    	if(list!=null&&list.size()>0){
    		for(int i=0;i<list.size();i++){
    			UserIndentMoneyTmp moneyTmp = (UserIndentMoneyTmp) list.get(i);
    			UserIndent indent = moneyTmp.getUserIndent();
    			List l = getBaseIndentTypeFromUserPayByIndentId(indent.getId());
    			if(l==null){
    				continue;
    			}else{
    				UserBaseIndentType base = (UserBaseIndentType) l.get(0);
    				Boolean flag = (Boolean) l.get(1);
    				
    				if(base.getStateType().equals("default")&&!indent.getState().equals(3)){
        				money += indent.getTotalPrice();
        			}else if(base.getStateType().equals("renzheng")&&flag){
        				money += moneyTmp.getMoney();
        			}
    			}
    			
    		}
    	}
    	ps.setTotalCosumption(money);
    	
    	//获得充值总额
    	List chongzhi = userManagerService.findUserChongzhiList(user);
    	Float chongzhiMoney = 0f;
    	if(chongzhi!=null&&chongzhi.size()>0){
    		for(int i=0;i<chongzhi.size();i++){
    			UserPay userPay = (UserPay) chongzhi.get(i);
    			chongzhiMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotleChongzhiMoney(chongzhiMoney);
    	//获得扣款总额
    	List koukuan = userManagerService.findUserKoukuanList(user);
    	Float koukuanMoney = 0f;
    	if(koukuan!=null&&koukuan.size()>0){
    		for(int i=0;i<koukuan.size();i++){
    			UserPay userPay = (UserPay) koukuan.get(i);
    			koukuanMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotleKoukuanMoney(koukuanMoney);
    	//获得返利总额
    	List fanli = userManagerService.findUserfanliList(user);
    	Float fanliMoney = 0f;
    	if(fanli!=null&&fanli.size()>0){
    		for(int i=0;i<fanli.size();i++){
    			UserPay userPay = (UserPay) fanli.get(i);
    			fanliMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotlefanliMoney(fanliMoney);
		return ps;
		
	}
	
	public PaginationSupport getFinancialRecords2(int pageName,User user){
		Session session = financialDao.getSession();
		Criteria criteria = session.createCriteria(UserPay.class);
		criteria.add(Restrictions.eq("childUser.userId", user.getUserId())).addOrder(
				Order.desc("id"));
		PaginationSupport ps =  abstractManager.findPageByCriteria(criteria, pageName);
		//获得消费总额
		List list = weiboIndentService.getUserConsumptionTotalMoney(user);
    	Float money = 0f;
    	if(list!=null&&list.size()>0){
    		for(int i=0;i<list.size();i++){
    			UserIndentMoneyTmp moneyTmp = (UserIndentMoneyTmp) list.get(i);
    			UserIndent indent = moneyTmp.getUserIndent();
    			List l = getBaseIndentTypeFromUserPayByIndentId(indent.getId());
    			if(l==null){
    				continue;
    			}else{
    				UserBaseIndentType base = (UserBaseIndentType) l.get(0);
    				Boolean flag = (Boolean) l.get(1);
    				
    				if(base.getStateType().equals("default")&&!indent.getState().equals(3)){
        				money += indent.getTotalPrice();
        			}else if(base.getStateType().equals("renzheng")&&flag){
        				money += moneyTmp.getMoney();
        			}
    			}
    			
    		}
    	}
    	ps.setTotalCosumption(money);
    	
    	//获得充值总额
    	List chongzhi = userManagerService.findUserChongzhiList(user);
    	Float chongzhiMoney = 0f;
    	if(chongzhi!=null&&chongzhi.size()>0){
    		for(int i=0;i<chongzhi.size();i++){
    			UserPay userPay = (UserPay) chongzhi.get(i);
    			chongzhiMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotleChongzhiMoney(chongzhiMoney);
    	//获得扣款总额
    	List koukuan = userManagerService.findUserKoukuanList(user);
    	Float koukuanMoney = 0f;
    	if(koukuan!=null&&koukuan.size()>0){
    		for(int i=0;i<koukuan.size();i++){
    			UserPay userPay = (UserPay) koukuan.get(i);
    			koukuanMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotleKoukuanMoney(koukuanMoney);
    	//获得返利总额
    	List fanli = userManagerService.findUserfanliList(user);
    	Float fanliMoney = 0f;
    	if(fanli!=null&&fanli.size()>0){
    		for(int i=0;i<fanli.size();i++){
    			UserPay userPay = (UserPay) fanli.get(i);
    			fanliMoney +=userPay.getMoney();
    		}
    	}
    	ps.setTotlefanliMoney(fanliMoney);
		return ps;
	}
}
